package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.ResultCode;
import com.pwt.common.ResultPage;
import com.pwt.entity.*;
import com.pwt.model.ProductManageModel;
import com.pwt.model.ProductManagePOJO;
import com.pwt.repository.ManageFeatureRepository;
import com.pwt.repository.ProductManageRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringTemplate;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ProductManage service层
 *
 * @author auto generated
 * @date 2021-08-31 12:03:28
 */
@Service
public class ProductManageService {

    private ProductManageRepository repository;

    private ManageFeatureRepository manageFeatureRepository;

    private JPAQueryFactory jpaQueryFactory;
    /**
     * 构造注入
     * @param repository
     */
    public ProductManageService(ProductManageRepository repository,ManageFeatureRepository manageFeatureRepository,JPAQueryFactory jpaQueryFactory){
        this.repository = repository;
        this.manageFeatureRepository = manageFeatureRepository;
        this.jpaQueryFactory = jpaQueryFactory;
    }


    public ResultPage getList(String name,Integer offset,Integer limit){
        return this.repository.getList(name,offset,limit);
    }

    @Transactional(rollbackFor = Exception.class)
    public ProductManage update(Integer teteil,String color) throws CustomException {
        ProductManage productManage = this.repository.getByTeteil(teteil);
        if(productManage == null){
            productManage = new ProductManage();
        }
        productManage.setTeteil(teteil);
        productManage.setColor(color);
        productManage = this.repository.save(productManage);
        if(productManage == null){
            throw new CustomException("添加失败！",new RuntimeException());
        }
        return productManage;
    }

}
